package finance

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/finance"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var RefundRecordGroup = core.Group("/finance/refund", newRecordHandler, regRecord, middleware.TokenAuth())

func newRecordHandler(srv finance.IRefundRecordService, refundLogSrv finance.IRefundLogService) *recordHandler {
	return &recordHandler{srv: srv, refundLogSrv: refundLogSrv}
}

func regRecord(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *recordHandler) {
		rg.GET("/record/list", handle.list)
		rg.GET("/record/stat", handle.stat)
		rg.GET("/log", handle.refundLog)
	})
}

type recordHandler struct {
	srv          finance.IRefundRecordService
	refundLogSrv finance.IRefundLogService
}

// list record列表
func (hd recordHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.RefundRecordListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(c, page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// 退款统计
func (hd recordHandler) stat(c *gin.Context) {

	res, err := hd.srv.Stat(c)
	response.CheckAndRespWithData(c, res, err)
}

// 退款日志
func (hd recordHandler) refundLog(c *gin.Context) {
	var listReq req.RefundLogListReq

	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.refundLogSrv.List(c, listReq)
	response.CheckAndRespWithData(c, res, err)
}
